package be.jaisy.osgi.console.ui.client.Bundle;

import java.util.Collection;
import java.util.Iterator;

import be.jaisy.osgi.console.service.api.IBundleManagementService;
import be.jaisy.osgi.console.service.api.IBundleManagementServiceAsync;
import be.jaisy.osgi.console.service.api.to.BundleTo;
import be.jaisy.osgi.console.ui.client.OsgiConsole;
import be.jaisy.ui.gwt.client.widget.Sink;
import be.jaisy.ui.gwt.client.widget.table.Table;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/**
 * Overview of the OSGI Console.
 */
public class Bundle extends Sink {

	public static SinkInfo init() {
		return new SinkInfo("Bundle", "<p>Control OSGI Bundles</p>") {

			public Sink createInstance() {
				return new Bundle();
			}

			public String getColor() {
				return "#bf2a2a";
			}

		};
	}

	/**
	 * An image provider to make available images to Bundle.
	 */
	public interface Images extends ImageBundle, TreeImages {
		AbstractImagePrototype plugin_add();

		AbstractImagePrototype plugin_delete();

		AbstractImagePrototype plugin_disabled();

		AbstractImagePrototype plugin_edit();

		AbstractImagePrototype plugin_error();

		AbstractImagePrototype plugin_go();

		AbstractImagePrototype plugin_link();

		AbstractImagePrototype plugin();
	}

	private final IBundleManagementServiceAsync service;

	private final Table table;

	private static final String[] headers = new String[] { "", "Id", "Name", "Version", "State" };

	public Bundle() {

		// Initialize the service.
		service = (IBundleManagementServiceAsync) GWT.create(IBundleManagementService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) service;
		endpoint.setServiceEntryPoint(OsgiConsole.MODULE_RELATIVE_PATH);

		table = new Table(headers);
		refreshData();

		initWidget(table);
	}

	public void onShow() {
		refreshData();
	}

	private void refreshData() {
		service.listBundle(new AsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(Object result) {
				Collection bundleTos = (Collection) result;
				RowFormatter rowFormatter = table.getRowFormatter();
				int rowIndex = 1;
				for (Iterator iter = bundleTos.iterator(); iter.hasNext();) {
					final BundleTo bundleTo = (BundleTo) iter.next();

					HorizontalPanel actions = new HorizontalPanel();
					
					Image image = null;
					if ("ACTIVE".equals(bundleTo.getState())) {
						 image = ((Images) GWT.create(Images.class)).plugin_delete().createImage();
						image.addMouseListener(new MouseListenerAdapter() {

							  public void onMouseDown(Widget sender, int x, int y) {
								  onClick(sender);
							  }
							
							public void onClick(Widget sender) {
								Window.alert("click on image");
								service.stopBundle(bundleTo.getBundleId().longValue(), new AsyncCallback() {

									public void onFailure(Throwable caught) {
										Window.alert(caught.getMessage());
									}

									public void onSuccess(Object result) {
										refreshData();
									}
								});
							}
						});
						actions.add(image);
					} else {
						 image = ((Images) GWT.create(Images.class)).plugin_add().createImage();
						image.addClickListener(new ClickListener() {

							public void onClick(Widget sender) {
								Window.alert("click on image for bundle:"+bundleTo.getBundleId().longValue());
								service.startBundle(bundleTo.getBundleId().longValue(), new AsyncCallback() {

									public void onFailure(Throwable caught) {
										Window.alert(caught.getMessage());
									}

									public void onSuccess(Object result) {
										refreshData();
									}
								});
							}

						});
						actions.add(image);
					}
					table.setValue(rowIndex, 0, image);
					table.setValue(rowIndex, 1, bundleTo.getBundleId());
					table.setValue(rowIndex, 2, bundleTo.getSymbolicName());
					table.setValue(rowIndex, 3, bundleTo.getVersion());
					table.setValue(rowIndex, 4, bundleTo.getState());				
					if (!"ACTIVE".equals(bundleTo.getState()))
						rowFormatter.setStyleName(rowIndex, "yellowRow");
					else
						rowFormatter.setStyleName(rowIndex, "tableRow");


					rowIndex++;
				}
			}

		});
	}
}
